using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;



namespace System.WebControls
{
    [DefaultProperty("Host")]
    [ToolboxData("<{0}:Contact runat=server></{0}:Contact>")]

    ///<summary>
    ///The class that creates the Contact Web Control.
    ///</summary>
    public class Contact : CompositeControl 
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        protected Label Label1;
        protected Label Label2;
        protected Label Label3;
        protected TextBox TextBox1;
        protected TextBox TextBox2;
        protected TextBox TextBox3;
        protected RequiredFieldValidator Field1;
        protected RequiredFieldValidator Field2;
        protected RequiredFieldValidator Field3;
        protected Button Button1;
        protected Button Button2;

        public Contact()
        {
        }

        /// <summary>
        /// The Host property
        /// </summary>
        public string Host
        {
            get
            {
                String s = (String)ViewState["Host"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Host"] = value;
            }
        }
        /// <summary>
        /// The To: property
        /// </summary>
        public string To
        {
            get
            {
                String s = (String)ViewState["To"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["To"] = value;
            }
        }
        /// <summary>
        /// The subject property
        /// </summary>
        public string Subject
        {
            get
            {
                String s = (String)ViewState["Subject"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Subject"] = value;
            }
        }
        #region Send Button Method
        /// <summary>
        /// The method that sends the email.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Send(object sender, System.EventArgs e)
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress(TextBox2.Text, TextBox1.Text);
            smtpClient.Host = Host;
            smtpClient.Port = 25;
            smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            message.From = fromAddress;
            message.To.Add(To);
            message.Subject = Subject;
            message.Body = TextBox3.Text;
            smtpClient.Send(message);

        }
        #endregion 
        /// <summary>
        /// The method that resets the textbox fields.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, System.EventArgs e)
        {
            TextBox1.Text = " ";
            TextBox2.Text = " ";
            TextBox3.Text = " ";
        }
        /// <summary>
        /// The method that creates the Contact webcontrol.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            //
            Label1 = new Label();
            Label1.Text = "From:";
            Controls.Add(Label1);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            //
            TextBox1 = new TextBox();
            TextBox1.ID = "TextBox1";
            TextBox1.Width = 178;
            Controls.Add(TextBox1);
            //
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Field1 = new RequiredFieldValidator();
            Field1.ControlToValidate = "TextBox1";
            Field1.ID = "Field1";
            Field1.ErrorMessage = "Please Enter Your Name";
            Controls.Add(Field1);
            Controls.Add(new LiteralControl("<br/>"));
            Controls.Add(new LiteralControl("<br/>"));
            //
            Label2 = new Label();
            Label2.Text = "Email:";            
            Controls.Add(Label2);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            //
            TextBox2 = new TextBox();
            TextBox2.ID = "TextBox2";
            TextBox2.Width = 178;            
            Controls.Add(TextBox2);
            //
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Field2 = new RequiredFieldValidator();            
            Field2.ControlToValidate = "TextBox2"; 
            Field2.ID = "Field2";
            Field2.ErrorMessage = "Please Enter a Vaild Email";
            Controls.Add(Field2);            
            Controls.Add(new LiteralControl("<br/>"));
            Controls.Add(new LiteralControl("<br/>"));
            //
            Label3 = new Label();
            Label3.Text = "Message:";
            Controls.Add(Label3);
            //
            TextBox3 = new TextBox();
            TextBox3.ID = "TextBox3";
            TextBox3.Height = 120;
            TextBox3.Width = 340;
            TextBox3.TextMode = TextBoxMode.MultiLine;
            Controls.Add(TextBox3);

            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            Field3 = new RequiredFieldValidator();
            Field3.ControlToValidate = "TextBox3";
            Field3.ID = "Field3";
            Field3.ErrorMessage = "Please Enter A Message";
            Controls.Add(Field3);
            Controls.Add(new LiteralControl("<br/>"));
            Controls.Add(new LiteralControl("<br/>"));
            //
            Button1 = new Button();
            Button1.Text = "Send";
            Button1.Click += new EventHandler(Send);
            Controls.Add(Button1);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(new LiteralControl("&nbsp;"));
            //
            Button2 = new Button();
            Button2.Text = "Reset";
            Button2.Click += new EventHandler(Button2_Click);
            Controls.Add(Button2);
        }

        public void DisableValidation(object sender, System.EventArgs e)
        {
            this.Field1.Visible = false;
            this.Field2.Visible = false;
            this.Field3.Visible = false;
        }

       

    }


}
